<?php

namespace App\Models\Live;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class ActivityModel extends Model
{
    use HasFactory;
    public static function sendGroupMsg($roomid,$config,$data){
        $server="https://console.tim.qq.com";
        $url="/v4/group_open_http_svc/send_group_system_notification?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json";
        $url=sprintf($url,$config['appid'],$config['adminname'],$config['adminkey'],$config['rand']);
        $body=[
            "GroupId"=>$data['roomid'],
            "Content"=>$data['msg']
        ];
        if(isset($data['to'])&&!empty($data['to'])){
            $body['ToMembers_Account']=$data['to'];
        }
        $s="URL:{$url}\r\n";
        $s.="BODY:".json_encode($body)."\r\n";
        $ret=Http::post($url,$body);
        $s.=$ret->body()."\r\n";
        $res=$ret->json();
        $v=0;
        if(isset($res['ActionStatus'])){
            $c=(int)$res['ErrorCode'];
            if(!empty($c)){
                $s.=$res['ErrorInfo'];
            }
            $v=$c;
        }else{
            $v=-1;
        }
        Log::debug($s);
        return $v;
    }
}
